/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sockets;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import ia1_practica3.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gracu
 */
public class HiloCliente extends Thread {

    static String HOST = "127.0.0.1";
    static final int PUERTO = 9999;
    Thread hilo;
    Socket skCliente;

    public HiloCliente() {
        try {
            skCliente = new Socket(HOST, PUERTO);
            System.out.println("ss: " + skCliente.toString());
        } catch (IOException ex) {
            Logger.getLogger(HiloCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        hilo = new Thread(this);
        hilo.start();
    }
    
    public HiloCliente(String IPHost) {
        try {
            skCliente = new Socket(IPHost, PUERTO);
            System.out.println("ss: " + skCliente.toString());
        } catch (IOException ex) {
            Logger.getLogger(HiloCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        hilo = new Thread(this);
        hilo.start();
    }
    
    public static void setIPServer(String IP){
        HOST = IP;
    }

    public void sendmessage(String enviar) {

        OutputStream auxe;
        try {
            auxe = skCliente.getOutputStream();
            DataOutputStream flujoe = new DataOutputStream(auxe);
            flujoe.writeUTF(enviar);
        } catch (IOException ex) {
            Logger.getLogger(HiloCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    boolean corriendo = true;

    @Override
    public void run() {
        while (corriendo) {
            try {
                // sendmessage();
                System.out.println("-----cliente");
                InputStream aux = skCliente.getInputStream();
                DataInputStream flujo = new DataInputStream(aux);
                String lectura = flujo.readUTF();

                String valor[] = lectura.split(";");
                String valor2[] = valor[0].split(",");
                String valor3[] = valor[1].split(",");
                Jugada jugada = new Jugada(Integer.parseInt(valor2[0]), Integer.parseInt(valor2[1]),
                        Integer.parseInt(valor3[0]), Integer.parseInt(valor3[0]), Integer.parseInt(valor[2]));

                //Main.getFormPrincipal().insertlista(jugada);


                System.out.println(lectura);

                //skCliente.close();
                Thread.sleep(100);
            } catch (Exception e) {

                System.out.println(e.getMessage());

            }
        }
    }

    public void parar(boolean corriendo) {
        this.corriendo = corriendo;
    }
}
